﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;

namespace XWord.WPF.ApplicationFramework.Threads
{
    public class GapDispatcherTimer
    {
        private DispatcherTimer gapValidateTimer;

        public static void BeginInvoke(double second, Action action)
        {
            new GapDispatcherTimer().Invoke(second, action);
        }

        public void Invoke(double second, Action action)
        {
            if (gapValidateTimer == null && second > 0)
            {
                gapValidateTimer = new DispatcherTimer();
                gapValidateTimer.Interval = TimeSpan.FromSeconds(second);
                gapValidateTimer.Tick += delegate
                {
                    action();
                    gapValidateTimer.Stop();
                    gapValidateTimer = null;
                };
            }
            if (second > 0)
                gapValidateTimer.Start();
        }
    }
}
